﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Sms.V20190711;
using TencentCloud.Sms.V20190711.Models;

namespace XQ.RequestOfficeSupplies.BLL.Common
{
    public class SMSHelper
    {
        public static string SendSMS(string phone, string name, string code)
        {
            try
            {
                Credential cred = new Credential
                {
                    SecretId = "AKID3wajbC7cTgfOhuuHRKwLVYOwDDwOoRAl",
                    SecretKey = "c2B4gTzLQEkyvb073hqiNma7NTKzOWN1"
                };

                ClientProfile clientProfile = new ClientProfile();
                HttpProfile httpProfile = new HttpProfile();
                httpProfile.Endpoint = ("sms.tencentcloudapi.com");
                clientProfile.HttpProfile = httpProfile;

                SmsClient client = new SmsClient(cred, "ap-shanghai", clientProfile);
                SendSmsRequest req = new SendSmsRequest();

                //模板参数
                List<string> tempParams = new List<string>();
                tempParams.Add(code);
                List<string> phoneStrs = new List<string>();

                req.TemplateID = "1285480";
                req.Sign = "羊尖镇廊下村民委员会";
                req.SmsSdkAppid = "1400722210";
                req.TemplateParamSet = tempParams.ToArray();

                phoneStrs.Add("+86" + phone);

                req.PhoneNumberSet = phoneStrs.ToArray();
                SendSmsResponse resp = client.SendSmsSync(req);

                DateTime dateTime = DateTime.Now;

                SendStatus[] status = resp.SendStatusSet;
                foreach (SendStatus sendStatus in status)
                {
                    //发送成功的记录发送日志
                    if (sendStatus.Code == "Ok")
                    {
                        return sendStatus.Code;
                    }
                }
                return "发送失败！";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        /// <summary>
        /// 发送指定内容短信
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static string SendSMS(string phone, string templateID)
        {
            try
            {
                Credential cred = new Credential
                {
                    SecretId = "AKID3wajbC7cTgfOhuuHRKwLVYOwDDwOoRAl",
                    SecretKey = "c2B4gTzLQEkyvb073hqiNma7NTKzOWN1"
                };

                ClientProfile clientProfile = new ClientProfile();
                HttpProfile httpProfile = new HttpProfile();
                httpProfile.Endpoint = ("sms.tencentcloudapi.com");
                clientProfile.HttpProfile = httpProfile;

                SmsClient client = new SmsClient(cred, "ap-shanghai", clientProfile);
                SendSmsRequest req = new SendSmsRequest();

                //模板参数
                //List<string> tempParams = new List<string>();
                List<string> phoneStrs = new List<string>();

                req.TemplateID = templateID;
                req.Sign = "羊尖镇廊下村民委员会";
                req.SmsSdkAppid = "1400722210";
                //req.TemplateParamSet = tempParams.ToArray();

                phoneStrs.Add("+86" + phone);

                req.PhoneNumberSet = phoneStrs.ToArray();
                SendSmsResponse resp = client.SendSmsSync(req);

                DateTime dateTime = DateTime.Now;

                SendStatus[] status = resp.SendStatusSet;
                foreach (SendStatus sendStatus in status)
                {
                    //发送成功的记录发送日志
                    if (sendStatus.Code == "Ok")
                    {
                        return sendStatus.Code;
                    }
                }
                return "发送失败！";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }


        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="tempParams"></param>
        /// <param name="tempID"></param>
        /// <returns></returns>
        public static string SendSMS(string phone, List<string> tempParams, string tempID)
        {
            try
            {
                Credential cred = new Credential
                {
                    SecretId = "AKID3wajbC7cTgfOhuuHRKwLVYOwDDwOoRAl",
                    SecretKey = "c2B4gTzLQEkyvb073hqiNma7NTKzOWN1"
                };

                ClientProfile clientProfile = new ClientProfile();
                HttpProfile httpProfile = new HttpProfile();
                httpProfile.Endpoint = ("sms.tencentcloudapi.com");
                clientProfile.HttpProfile = httpProfile;

                SmsClient client = new SmsClient(cred, "ap-shanghai", clientProfile);
                SendSmsRequest req = new SendSmsRequest();

                List<string> phoneStrs = new List<string>();

                req.TemplateID = tempID;
                req.Sign = "羊尖镇廊下村民委员会";
                req.SmsSdkAppid = "1400634678";
                req.TemplateParamSet = tempParams.ToArray();

                phoneStrs.Add("+86" + phone);

                req.PhoneNumberSet = phoneStrs.ToArray();
                SendSmsResponse resp = client.SendSmsSync(req);

                DateTime dateTime = DateTime.Now;

                SendStatus[] status = resp.SendStatusSet;
                foreach (SendStatus sendStatus in status)
                {
                    //发送成功的记录发送日志
                    if (sendStatus.Code == "Ok")
                    {
                        return sendStatus.Code;
                    }
                }
                return "发送失败！";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }
}
